//
//  ZHNetworkHandler.m
//  DelightTrip
//
//  Created by dllo on 16/5/11.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "ZHNetworkHandler.h"
#import "AFNetworking.h"
#import "AFHTTPSessionManager.h"

@implementation ZHNetworkHandler
+ (void)getDataByURLString:(NSString *)urlString WithBodyString:(NSString *)bodyString WithHeaderDic:(NSDictionary *)headerDic WithResponse:(ZHResponseStyle)response WithSuccess:(blockOfSuccess)success WithFailure:(blockOfFailure)failure {
    //1)获取manager对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //2)请求头的设置
    if (headerDic) {
        for (NSString *key in headerDic.allKeys) {
            [manager.requestSerializer setValue:headerDic[key] forHTTPHeaderField:key];
        }
    }
    //3)设置返回数据的类型
    switch (response) {
        case ZHJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case ZHDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case ZHXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    //4)设置相应数据类型
    manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil];
    
    //5)iOS9 汉字转码
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    //请求Https
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    //6)发送请求
    [manager GET:urlString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
        NSLog(@"请求失败,原因是%@", error);
    }];
}

+ (void)postDataByURLString:(NSString *)urlString WithBodyString:(NSString *)bodyString WithBodyStyle:(ZHRequestStyle)bodyStyle WithHeaderDic:(NSDictionary *)headerDic WithResponse:(ZHResponseStyle)response WithSuccess:(blockOfSuccess)success WithFailure:(blockOfFailure)failure {
    //1)获取manager对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //2)设置body数据类型
    switch (bodyStyle) {
        case ZHBodyJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case ZHBodyString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
                return parameters;
            }];
            break;
        default:
            break;
    }
    
    //3)请求头的设置
    if (headerDic) {
        for (NSString *key in headerDic.allKeys) {
            [manager.requestSerializer setValue:headerDic[key] forHTTPHeaderField:key];
        }
    }
    
    //4)设置返回数据的类型
    switch (response) {
        case ZHJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case ZHDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case ZHXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    //5)设置相应数据类型
    manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil];
    
    //6)iOS9 汉字转码
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    //请求Https
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    //7)发送请求
    [manager POST:urlString parameters:bodyString progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
        NSLog(@"请求失败,原因是%@", error);
    }];
}

@end
